package com.sourny.auth.configuration;

import com.sourny.auth.filter.SecurityAuthFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * 自动配置类
 *
 * @author sourny.yang
 * @date 18:04 2018/6/12
 */
@Configuration
@ComponentScan("com.ksudi.common.mvc.auth")
@EnableConfigurationProperties(SecurityAuthProperties.class)
public class SecurityAuthAutoConfiguration {


    @Autowired
    private SecurityAuthProperties authProperties;

    @Bean
    public FilterRegistrationBean<SecurityAuthFilter> securityAuthFilterFilterRegistrationBean() {
        SecurityAuthFilter securityAuthFilter = new SecurityAuthFilter(authProperties);

        FilterRegistrationBean<SecurityAuthFilter> securityAuthRegister = new FilterRegistrationBean<>(securityAuthFilter);

        securityAuthRegister.setName("securityAuthFilter");

        securityAuthRegister.addUrlPatterns("/*");

        // 只生成token 则禁用校验过滤器
        if (authProperties.isOnlyGenerateToken()) {
            securityAuthRegister.setEnabled(false);
        }

        return securityAuthRegister;

    }
}
